﻿





using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using System.Linq.Dynamic.Core;

using KDS.Mes.Mes.TestPower2s.Dtos;
using KDS.Mes.Dto;
using KDS.Mes.Authorization;

namespace KDS.Mes.Mes.TestPower2s
{
    /// <summary>
    /// 2线电检数据表服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Report_TestPower2s)]
    public class TestPower2AppService : MesAppServiceBase, ITestPower2AppService
    {
        private readonly IRepository<TestPower2, int> _testPower2Repository;
        private readonly ITestPower2ListExcelExporter _testPower2ListExcelExporter;


        private readonly TestPower2Manage _testPower2Manage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public TestPower2AppService(IRepository<TestPower2, int> testPower2Repository,
TestPower2Manage testPower2Manage
      , ITestPower2ListExcelExporter testPower2ListExcelExporter
  )
        {
            _testPower2Repository = testPower2Repository;
            _testPower2Manage = testPower2Manage;
            _testPower2ListExcelExporter = testPower2ListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<TestPower2> _testPower2RepositoryAsNoTrack => _testPower2Repository.GetAll().AsNoTracking();


        #endregion


        #region 2线电检数据表管理

        public PagedResultDto<TestPower2ListDto> GetPagedTestPower2s(GetTestPower2Input input)
        {
            //初步过滤
            var query = _testPower2Repository.GetAll()
                        .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                        .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                        .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2);
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CREAT_TIME);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();
            return new PagedResultDto<TestPower2ListDto>(tasksCount, taskList.MapTo<List<TestPower2ListDto>>());
        }

        /// <summary>
        /// 根据查询条件获取2线电检数据表分页列表
        /// </summary>
        public async Task<PagedResultDto<TestPower2ListDto>> GetPagedTestPower2sAsync(GetTestPower2Input input)
        {

            var query = _testPower2RepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var testPower2Count = await query.CountAsync();

            var testPower2s = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var testPower2ListDtos = testPower2s.MapTo<List<TestPower2ListDto>>();
            return new PagedResultDto<TestPower2ListDto>(
            testPower2Count,
            testPower2ListDtos
            );
        }

        /// <summary>
        /// 通过Id获取2线电检数据表信息进行编辑或修改 
        /// </summary>
        public async Task<GetTestPower2ForEditOutput> GetTestPower2ForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetTestPower2ForEditOutput();

            TestPower2EditDto testPower2EditDto;

            if (input.Id.HasValue)
            {
                var entity = await _testPower2Repository.GetAsync(input.Id.Value);
                testPower2EditDto = entity.MapTo<TestPower2EditDto>();
            }
            else
            {
                testPower2EditDto = new TestPower2EditDto();
            }

            output.TestPower2 = testPower2EditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取2线电检数据表ListDto信息
        /// </summary>
        public async Task<TestPower2ListDto> GetTestPower2ByIdAsync(EntityDto<int> input)
        {
            var entity = await _testPower2Repository.GetAsync(input.Id);

            return entity.MapTo<TestPower2ListDto>();
        }







        /// <summary>
        /// 新增或更改2线电检数据表
        /// </summary>
        public async Task CreateOrUpdateTestPower2Async(CreateOrUpdateTestPower2Input input)
        {
            if (input.TestPower2EditDto.Id.HasValue)
            {
                await UpdateTestPower2Async(input.TestPower2EditDto);
            }
            else
            {
                await CreateTestPower2Async(input.TestPower2EditDto);
            }
        }

        /// <summary>
        /// 新增2线电检数据表
        /// </summary>
        public virtual async Task<TestPower2EditDto> CreateTestPower2Async(TestPower2EditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<TestPower2>();

            entity = await _testPower2Repository.InsertAsync(entity);
            return entity.MapTo<TestPower2EditDto>();
        }

        /// <summary>
        /// 编辑2线电检数据表
        /// </summary>
        public virtual async Task UpdateTestPower2Async(TestPower2EditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _testPower2Repository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _testPower2Repository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除2线电检数据表
        /// </summary>
        public async Task DeleteTestPower2Async(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _testPower2Repository.DeleteAsync(input.Id);
        }

        #endregion
        #region 2线电检数据表的Excel导出功能


        public async Task<FileDto> GetTestPower2ToExcel(GetTestPower2Input input)
        {
            var entities = await _testPower2Repository.GetAll()
                .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                        .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                        .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2)
                .ToListAsync();

            var dtos = entities.MapTo<List<TestPower2ListDto>>();

            var fileDto = _testPower2ListExcelExporter.ExportTestPower2ToFile(dtos);



            return fileDto;
        }


        #endregion










    }
}
